<?php
/**
 * Notes:
 * User: LiaoHong
 * Date: 2025/10/20
 * Time: 9:04
 **/
namespace App\Services\Auth;
use App\Exceptions\ServiceException;
use App\Models\Auth\Menu;
use App\Models\User\User;
use Illuminate\Support\Facades\DB;
use \Exception;

class MenuService
{


    /**
     * @Notes:获取菜单结构树
     * @author: Liao Hong
     * @Time: 2025/10/21   17:49
     * @Interface treeselect
     */
    private $title = '菜单管理';
    public function treeselect()
    {
      $row = Menu::query()->select(['menu_id as id','menu_name','parent_id'])->get()->toArray();
      $result = buildTree($row);

      return $result;
    }
    /**
     * @Notes:添加菜单
     * @param $params
     * @return bool
     * @author: Liao Hong
     * @Time: 2025/10/20   11:11
     * @Interface add
     */
    public function add($params)
    {
        $row = Menu::query()->insert($params);
        addLog($this->title,'1',$row);
        return $row;
    }

    /**
     * @Notes:获取单条菜单信息
     * @param $params
     * @author: Liao Hong
     * @Time: 2025/10/21   14:31
     * @Interface getRow
     */
    public function getRow($params)
    {
        return Menu::query()->find($params['menu_id'])->toArray();
    }
    /**
     * @Notes:修改部门
     * @param $parmas
     * @author: Liao Hong
     * @Time: 2025/10/20   11:12
     * @Interface update
     */
    public function update($parmas){

        $row = Menu::query()->where('menu_id',$parmas['menu_id'])->update($parmas);
        addLog($this->title,'2',$row);
        return $row;
    }
    /**
     * @Notes:删除部门
     * @param $parmas
     * @author: Liao Hong
     * @Time: 2025/10/20   11:12
     * @Interface update
     */
    public function delete($parmas){
        $row =  Menu::query()->where('menu_id',$parmas['menu_id'])->delete();
        addLog($this->title,'3',$row);
        return $row;
    }
    /**
     * @Notes:部门列表
     * @param $parmas
     * @author: Liao Hong
     * @Time: 2025/10/20   11:12
     * @Interface update
     */
    public function list($parmas){
      $query = [];
      if(isset($parmas['menu_name']) && !empty($parmas['menu_name'])){
          $query[] = ['menu_name','like','%'.$parmas['menu_name'].'%'];
      }
      if(isset($parmas['status']) && !empty($parmas['status'])){
          $query[] = ['status','=',$parmas['status']];
      }

      $row = Menu::query()->where($query)->get();
      $row = empty($row) ?  [] :$row->toArray();
      return $row;
    }

}
